import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import AdoptionBrowse from '@/views/AdoptionBrowse.vue'
import PetDetail from '@/views/PetDetail.vue'
import MyApplications from '@/views/MyApplications.vue'
import Profile from '@/views/Profile.vue'
import RescueSubmit from '@/views/RescueSubmit.vue'
import RescueDetail from '@/views/RescueDetail.vue'
import MyRescues from '@/views/MyRescues.vue'
import Forum from '@/views/Forum.vue'
import PostDetail from '@/views/PostDetail.vue'
import CreatePost from '@/views/CreatePost.vue'
import MyPosts from '@/views/MyPosts.vue'
import FavoritePosts from '@/views/FavoritePosts.vue'
import Rescues from '@/views/Rescues.vue'
import ComingSoon from '@/views/ComingSoon.vue'
import NotFound from '@/views/NotFound.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { title: '首页' }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录', requiresGuest: true }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { title: '注册', requiresGuest: true }
  },
  {
    path: '/adoption-browse',
    name: 'AdoptionBrowse',
    component: AdoptionBrowse,
    meta: { title: '宠物领养浏览', requiresAuth: true }
  },
  {
    path: '/pet-detail/:id',
    name: 'PetDetail',
    component: PetDetail,
    meta: { title: '宠物详情', requiresAuth: true }
  },
  {
    path: '/my-applications',
    name: 'MyApplications',
    component: MyApplications,
    meta: { title: '我的领养申请', requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: { title: '个人中心', requiresAuth: true }
  },
  {
    path: '/my-rescue',
    name: 'MyRescue',
    component: () => import('../views/MyRescues.vue'),
    meta: { title: '我发布的救助' }
  },
  {
    path: '/rescues',
    name: 'Rescues',
    component: Rescues,
    meta: { title: '宠物救助信息' }
  },
  {
    path: '/rescue-submit',
    name: 'RescueSubmit',
    component: RescueSubmit,
    meta: { title: '宠物救助信息', requiresAuth: true }
  },
  {
    path: '/rescue-detail/:id',
    name: 'RescueDetail',
    component: RescueDetail,
    meta: { title: '救助信息详情' }
  },
  {
    path: '/my-rescues',
    name: 'MyRescues',
    component: MyRescues,
    meta: { title: '我的救助信息', requiresAuth: true }
  },
  {
    path: '/forum',
    name: 'Forum',
    component: Forum,
    meta: { title: '宠物交流论坛' }
  },
  {
    path: '/forum/post/:id',
    name: 'PostDetail',
    component: PostDetail,
    meta: { title: '帖子详情' }
  },
  {
    path: '/forum/create',
    name: 'CreatePost',
    component: CreatePost,
    meta: { title: '发布帖子', requiresAuth: true }
  },
  {
    path: '/forum/edit/:id',
    name: 'EditPost',
    component: CreatePost,
    meta: { title: '编辑帖子', requiresAuth: true }
  },
  {
    path: '/forum/my-posts',
    name: 'MyPosts',
    component: MyPosts,
    meta: { title: '我的帖子', requiresAuth: true }
  },
  {
    path: '/forum/favorites',
    name: 'FavoritePosts',
    component: FavoritePosts,
    meta: { title: '我的收藏', requiresAuth: true }
  },
  // 404页面
  {
    path: '*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: { title: '页面未找到' }
  }
]

// 创建路由实例
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject)
  }
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

export default router